# coding: utf-8


from django.conf import settings
from .. import models
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkecs.v2.ecs_client import EcsClient
from huaweicloudsdkecs.v2.model.list_servers_details_request import ListServersDetailsRequest
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion

huaweiregion = {
    "cn-east-2": "华东-上海二",
    "cn-east-3": "华东-上海一",
    "cn-south-1":  "华南-广州",

    "cn-southwest-2":  "西南-贵阳一",

    "ap-southeast-2": "亚太-曼谷",

    "ap-southeast-1":  "亚太-香港",

    "ap-southeast-3":  "亚太-新加坡",
    "af-south-1": "非洲-约翰内斯堡",

    "cn-north-4": "华北-北京四",

    "cn-north-1": "华北-北京一",


}
# 华为云登录


class novalEcsHost():
    def __init__(self):
        pass

    def getEcs(requestdata):
        # 选择获取
        for k, v in huaweiregion.items():
            if v == str(requestdata):
                # 连接华为云api接口
                credentials = BasicCredentials(settings.AK, settings.SK)
                # 获取ecs主机信息
                ecsclient = EcsClient.new_builder().with_credentials(
                    credentials).with_region(EcsRegion.value_of(k)).build()
                try:
                    request = ListServersDetailsRequest()
                    # 设置查询的限制和分页，默认limit = 25 offset 无上限
                    request.limit = 100
                    request.offset = 1
                    response = eval(
                        str(ecsclient.list_servers_details(request)))
                    # 获取所有参数

                    # 获取接口参数中服务器信息
                    ecsservers = response["servers"]
                    # print("ecsservers________", ecsservers)
                    # 获取主机IP,公网IP和私网IP

                    for paramer in ecsservers:

                        addresss = paramer["addresses"]
                        ipaddres = []
                        for addreValue in addresss:

                            # 获取values
                            for addres in addresss[addreValue]:
                                # print(addres["addr"])

                                ipaddres.append(addres["addr"])

                    # 临时组合当前数据
                            # tempParam = {"name": paramer["name"], "addresses": ipaddres, "host_status": paramer["host_status"],
                            #              "created": paramer["created"], "ram": paramer["flavor"]["ram"], "vcpus": paramer["flavor"]["vcpus"], "image_name": paramer["metadata"]["image_name"], "os_bit": paramer["metadata"]["os_bit"], "os_type": paramer["metadata"]["os_type"]}
                            # print(tempParam)
                            models.novalListEcs.objects.create(
                                ecs_name=paramer["name"], ecs_host_id=paramer["id"], ecs_host_ip=ipaddres, ecs_host_memory=paramer["flavor"]["ram"], ecs_host_cpu=paramer["flavor"]["vcpus"], ecs_host_os=paramer["metadata"]["image_name"], ecs_host_bit=paramer["metadata"]["os_bit"], ecs_host_type=paramer["metadata"]["os_type"], ecs_host_status=paramer["host_status"], ecs_host_date=paramer["created"])
                    # print(tempParam)
                    return ecsservers

                except exceptions.ClientRequestException as e:
                    print(e.status_code)
                    print(e.request_id)
                    print(e.error_code)
                    print(e.error_msg)

    def getEcsVolum(requestdata):
        pass

    def getEcsInfo(self):
        pass
